package com.bird.ecmainmenu3.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bird.ecmainmenu3.R;
import com.bird.ecmainmenu3.base.OnAppStateListener;
import com.bird.ecmainmenu3.base.OnItemClickListener;
import com.bird.ecmainmenu3.model.AppInfo;
import com.bird.ecmainmenu3.model.AppListAdapter;
import com.bird.ecmainmenu3.presenter.AppsManager;
import com.bird.ecmainmenu3.util.Utils;

import java.util.ArrayList;
import java.util.List;

public class AppListActivity extends AppCompatActivity implements OnItemClickListener, OnAppStateListener {
    private static final String TAG = Utils.TAG + AppListActivity.class.getSimpleName();
    private static final int SPAN_COUNT = 5;

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    private final List<AppInfo> mAppList = new ArrayList<>();
    private AppListAdapter mAppListAdapter;
    private RecyclerView mRecyclerView;
    private boolean mIsOpen = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_list_layout);
        init();
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();

        mAppList.clear();
        mAppList.addAll(new ArrayList<>(AppsManager.getInstance().getAppList()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void init() {
        initViews();
        initData();
    }

    private void initViews() {
        mRecyclerView = findViewById(R.id.app_list_id);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,SPAN_COUNT));
    }

    private void initData() {
        AppsManager.getInstance().setOnAppStateListener(this);
        mAppListAdapter = new AppListAdapter(mAppList, this);
        mRecyclerView.setAdapter(mAppListAdapter);
    }

    private void updateAppsListView() {
        if (mAppListAdapter != null) {
            mAppListAdapter.updateAppInfoList(mAppList);
            Log.d(TAG, "updateAppListView: " + mAppList.size());
        }
    }

    @Override
    public void onItemClick(int position) {
        Log.d(TAG, "onItemClick: position=" + position);
        if (0 <= position && position < mAppList.size()) {
            AppInfo appInfo = mAppList.get(position);
            if (appInfo != null) {
                String cls = appInfo.getClsName();
                String pkg = appInfo.getPackageName();
                Log.d(TAG, "onItemClick: pkg=" + pkg + ", cls=" + cls);
                AppsManager.getInstance().startApp(this, pkg, cls);
            } else {
                Log.e(TAG, "onItemClick: appInfo is null");
            }
        } else {
            Log.e(TAG, "onItemClick: position is invalid");
        }
    }

    @Override
    public void onNotifyClose() {
        Log.d(TAG, "onNotifyClose: ");
        if (mIsOpen) {
            mIsOpen = false;
            this.finish();
        }
    }

    @Override
    public void onNotifyOpen() {
        mIsOpen = true;
        Log.d(TAG, "onNotifyOpen: " + mIsOpen);
    }

    @Override
    public void onAppAdded(AppInfo appInfo) {
        mAppList.add(appInfo);
        mHandler.post(this::updateAppsListView);
        String packageName = appInfo.getPackageName();
        Utils.saveAppData(getApplicationContext(), packageName, packageName);
    }

    @Override
    public void onAppRemoved(AppInfo appInfo) {
        int position = mAppList.indexOf(appInfo);
        if (position >= 0) {
            mAppList.remove(appInfo);
            mHandler.post(this::updateAppsListView);
            String packageName = appInfo.getPackageName();
            Utils.removeAppData(getApplicationContext(), packageName);
        }
    }

    @Override
    public void onUpdateApps(List<AppInfo> list) {
        int size = list.size();
        Log.d(TAG, "list: size=" + size);

        mAppList.clear();
        mAppList.addAll(list);

        size = mAppList.size();
        Log.d(TAG, "mAppList: size=" + size);

        mHandler.post(this::updateAppsListView);
    }
}